<?php

/**
 * This is the model class for table "{{news}}".
 *
 * The followings are the available columns in table '{{news}}':
 * @property string $id
 * @property string $memberid
 * @property string $classid
 * @property string $title
 * @property string $author
 * @property string $source
 * @property integer $ispic
 * @property integer $isrecommend
 * @property integer $isshow
 * @property integer $istop
 * @property integer $isactive
 * @property integer $ishot
 * @property integer $isverify
 * @property string $visit
 * @property string $createtime
 * @property string $updatetime
 * @property string $releasename
 */
class News extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return News the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{news}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('classid, title', 'required'),
			array('ispic, isrecommend, isshow, istop, isactive, ishot, isverify', 'numerical', 'integerOnly'=>true),
			array('memberid, classid, visit', 'length', 'max'=>11),
			array('title', 'length', 'max'=>80),
			array('author', 'length', 'max'=>25),
			array('source, releasename', 'length', 'max'=>50),
			array('createtime, updatetime', 'length', 'max'=>11),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, memberid, classid, title, author, source, ispic, isrecommend, isshow, istop, isactive, ishot, isverify, visit, createtime, updatetime, releasename', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'content'=>array(self::HAS_ONE, 'NewsContent', 'newsid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'memberid' => 'Memberid',
			'classid' => 'Classid',
			'title' => 'Title',
			'author' => 'Author',
			'source' => 'Source',
			'ispic' => 'Ispic',
			'isrecommend' => 'Isrecommend',
			'isshow' => 'Isshow',
			'istop' => 'Istop',
			'isactive' => 'Isactive',
			'ishot' => 'Ishot',
			'isverify' => 'Isverify',
			'visit' => 'Visit',
			'createtime' => 'Createtime',
			'updatetime' => 'Updatetime',
			'releasename' => 'Releasename',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('memberid',$this->memberid,true);
		$criteria->compare('classid',$this->classid,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('author',$this->author,true);
		$criteria->compare('source',$this->source,true);
		$criteria->compare('ispic',$this->ispic);
		$criteria->compare('isrecommend',$this->isrecommend);
		$criteria->compare('isshow',$this->isshow);
		$criteria->compare('istop',$this->istop);
		$criteria->compare('isactive',$this->isactive);
		$criteria->compare('ishot',$this->ishot);
		$criteria->compare('isverify',$this->isverify);
		$criteria->compare('visit',$this->visit,true);
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('updatetime',$this->updatetime,true);
		$criteria->compare('releasename',$this->releasename,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave()
	{
		if ($this->isNewRecord)
		{
			$this->setAttribute('createtime', new CDbExpression('NOW()'));
			$this->setAttribute('releasename', 'admin');
		}
		$this->setAttribute('updatetime', new CDbExpression('NOW()'));
		return parent::beforeSave();
	}
}